﻿using System;
using System.Threading;
using System.Windows.Input;
using Softelligent.Zintra.ClientSDK;
using Softelligent.Zintra.Demos.Events.PublishShell.Commands;
using Softelligent.Zintra.Demos.Events.PublishShell.Models;
using Softelligent.Zintra.Demos.Events.PublishShell.Properties;

namespace Softelligent.Zintra.Demos.Events.PublishShell.ViewModels
{
    public class MainViewModel : ViewModel
    {
        private readonly Guid ProductId = new Guid("e5c0a609-9156-4a51-91fa-9f0100e9cb1c");
        private const string EventName = "TelephoneCall";

        public MainViewModel()
        {
            ActivateCommand = new DelegateCommand(OnActivate);
            PublishCommand = new DelegateCommand(OnPublish);
        }

        public ICommand ActivateCommand { get; set; }
        public ICommand PublishCommand { get; set; }

        private string activationError;
        public string ActivationError
        {
            get { return activationError; }
            set
            {
                if (activationError == value) return;
                activationError = value;
                NotifyPropertyChanged("ActivationError");
            }
        }

        private Guid activationKey;
        public string ActivationKey
        {
            get { return activationKey.ToString(); }
            set
            {
                var newActivationKey = Guid.Empty;
                Guid.TryParse(value, out newActivationKey);
                if (activationKey == newActivationKey) return;
                activationKey = newActivationKey;
                NotifyPropertyChanged("ActivationKey");
            }
        }

        private string publishError;
        public string PublishError
        {
            get { return publishError; }
            set
            {
                if (publishError == value) return;
                publishError = value;
                NotifyPropertyChanged("PublishError");
            }
        }

        private string log;
        public string Log
        {
            get { return log; }
            set
            {
                if (log == value) return;
                log = value;
                NotifyPropertyChanged("Log");
            }
        }

        private string fromNumber;
        public string FromNumber
        {
            get { return fromNumber; }
            set
            {
                if (fromNumber == value) return;
                fromNumber = value;
                NotifyPropertyChanged("FromNumber");
            }
        }

        private string toNumber;
        public string ToNumber
        {
            get { return toNumber; }
            set
            {
                if (toNumber == value) return;
                toNumber = value;
                NotifyPropertyChanged("ToNumber");
            }
        }

        private void OnActivate()
        {
            if (activationKey == Guid.Empty)
            {
                ActivationError = "You must input a valid activation key of this product before continueing.";
                return;
            }

            var context = SynchronizationContext.Current;
            Resource.Products.UsingJSON.Activate(
                activation => context.Post(state =>
                {
                    Settings.Default.Token = activation.Token;
                    Settings.Default.Save();
                    ActivationKey = string.Empty;
                    ActivationError = string.Empty;
                }, null),
                error => context.Post(state =>
                {
                    ActivationError = "Activation failed, please try again later.";
                }, null),
                ProductId, activationKey);

            //try
            //{
            //service.Activate(activationKey, (status, subscriptionId) => context.Post(state =>
            //{
            //    if (status == CallStatus.Success)
            //    {
            //        Settings.Default.SubscriptionId = subscriptionId.ToString();
            //        Settings.Default.Save();
            //        ActivationError = string.Empty;
            //    }
            //    else
            //    {
            //        ActivationError = "Activation failed, please try again later.";
            //    }
            //}, null));
            //}
            //catch (WebException)
            //{
            //    ActivationError = "There was a problem activating, please try again later.";
            //}
        }

        private void OnPublish()
        {
            var context = SynchronizationContext.Current;
            var xmlMessage = new TelephoneCall
            {
                FromNumber = fromNumber,
                ToNumber = toNumber
            };

            Resource.Events.UsingXML.AuthenticateWith(Settings.Default.Token).Publish(
                nothing => context.Post(state =>
                {
                    PublishError = string.Empty;
                    Log += string.Format("Publish from {0} to {1}  on  {2}.\r\n", fromNumber, toNumber, DateTime.Now.ToString("MMMM dd, yyyy  HH:mm:ss"));
                }, null),
                error => context.Post(state =>
                {
                    PublishError = "Publishing failed, please try again later.";
                }, null),
                EventName, xmlMessage);

            //try
            //{
            //service.Publish(fromNumber, toNumber, status => context.Post(state =>
            //{
            //    if (status == CallStatus.Success)
            //    {
            //        PublishError = string.Empty;
            //        Log += string.Format("Publish from {0} to {1}  on  {2}.\r\n", fromNumber, toNumber, DateTime.Now.ToString("MMMM dd, yyyy  HH:mm:ss"));
            //    }
            //    else
            //    {
            //        PublishError = "Publishing failed, please try again later.";
            //    }
            //}, null));
            //}
            //catch (WebException)
            //{
            //    ActivationError = "Publishing failed, please try again later.";
            //}
        }
    }
}
